#ifndef _XLOG_H_
#define _XLOG_H_

/**
 * @file xlog.h
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-03-17
 * 
 * @copyright Copyright (c) 2023
 * ref. https://blog.csdn.net/qq_39977813/article/details/101224720?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-101224720-blog-52083507.pc_relevant_recovery_v2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-101224720-blog-52083507.pc_relevant_recovery_v2&utm_relevant_index=1
 * ref. https://www.armbbs.cn/forum.php?mod=viewthread&tid=86172
 * ref. https://www.cnblogs.com/zhaoo-o/articles/17280529.html [the method for printf chinese charactor error]
 */

#include "compile_macros.h"

/* rtt */
#include "SEGGER_RTT.h"
#include "usart.h"

#define RTT_DBG_ENABLE          (1)
#define SIMPLE_LOG              (1)

enum log_level {LOG_LEVEL_ERROR, LOG_LEVEL_WARN, LOG_LEVEL_TRACE, LOG_LEVEL_DEBUG, LOG_LEVEL_INFO};
#define G_LOG_LEVEL (LOG_LEVEL_INFO)

#if(XLOG_STYLE == 1U)  /* XLOG_STYLE: RTT */
/* ref. https://blog.csdn.net/yaq_30401/article/details/124296839 */

/* log init */
#define XLOG_INIT()    SEGGER_RTT_Init()
/* RTT log output */
#define RTT_DBG_PORT        0
#define XLOG(level_str,color,arg,...)                             \
        SEGGER_RTT_printf(RTT_DBG_PORT,"%s[%s] "arg"\r\n%s",    \
                        color,                                  \
                        level_str,                                   \
                        ##__VA_ARGS__,                          \
                        RTT_CTRL_RESET)
/* log clear */
#define XLOG_CLEAR()     SEGGER_RTT_WriteString(RTT_DBG_PORT, "  "RTT_CTRL_CLEAR)

/****************************************************************************************************************/
#elif(XLOG_STYLE == 2U)  /* XLOG_STYLE: USART */
#define USART1_BAUD    (115200UL)
/* usart init */
#define XLOG_INIT()    usart_init(USART1_BAUD)
/* usart log output */
#define XLOG(level_str,color,arg,...)                             \
{        printf("[%s]", level_str);       \
		 printf(arg, ##__VA_ARGS__);      \
		 printf("\r\n");					\
}

/* usart clear */
#define XLOG_CLEAR() 

#else  /* XLOG_STYLE: NONE */

#define XLOG_INIT()
#define XLOG(level_str,color,arg,...) 
#define XLOG_CLEAR()

#endif


#if(SIMPLE_LOG == 1)

    #define LOG_COMMON(level_str, color, level, args,...) do { \
    if ( level <= G_LOG_LEVEL ) { \
        XLOG(level_str, color, args, ##__VA_ARGS__); \
    } \
    } while(0)

#else

    #define LOG_COMMON(level_str, level, args...) do { \
    if ( level >= g_log_level ) { \
        XLOG("| %20s | %5s | %s:%d(%s)# ", level_str, __FILE_NAME__, __LINE__, __FUNCTION__); \
        XLOG(args); \
        XLOG("\n"); \
    } \
    } while(0)

#endif


#define LOGI(args,...) LOG_COMMON( "INFO ", RTT_CTRL_TEXT_WHITE, LOG_LEVEL_INFO, args, ##__VA_ARGS__)
#define LOGD(args,...) LOG_COMMON( "DEBUG", RTT_CTRL_TEXT_BRIGHT_WHITE, LOG_LEVEL_DEBUG, args, ##__VA_ARGS__)
#define LOGT(args,...) LOG_COMMON( "TRACE", RTT_CTRL_TEXT_BRIGHT_CYAN, LOG_LEVEL_TRACE, args, ##__VA_ARGS__)
#define LOGW(args,...) LOG_COMMON( "WARN ", RTT_CTRL_TEXT_YELLOW, LOG_LEVEL_WARN, args, ##__VA_ARGS__)
#define LOGE(args,...) LOG_COMMON( "ERROR", RTT_CTRL_TEXT_BRIGHT_RED, LOG_LEVEL_WARN, args, ##__VA_ARGS__)



#endif
